import * as qs from "qs";
import * as Config from "@/config";
import { post, postForFile } from "@/api/httpRequest";

const BASE_URI = Config.HTTP_SERVER + "/kgeditor";

/**
 * 添加节点
 * @param data 格式 {gId:"图id", label: "标签", type: "类型" }
 * @param success
 * @param error
 * @returns {*}
 */
export function addNodeAPI(data, success, error, final) {
  post(BASE_URI + "/addNode",data,success,error,final);
}

/**
 * 添加连接
 * @param data
 * @param success
 * @param error
 * @returns {Promise<void>}
 */
export function addLinkAPI(data, success, error, final) {
  post(BASE_URI + "/addLink",data,success,error,final);
}

/**
 * 修改节点
 * label & type 如果未修改则不存在这两个属性
 * @param data
 * @param success
 * @param error
 */
export function editNodeAPI(data, success, error, final) {
  post(BASE_URI + "/fixNode",data,success,error,final);
}

/**
 * 修改连接
 * @param data
 * @param success
 * @param error
 */
export function editLinkAPI(data, success, error, final) {
  post(BASE_URI + "/fixLink",data,success,error,final);
}

/**
 * 删除节点
 * @param data
 * @param success
 * @param error
 */
export function deleteNodeAPI(data, success, error, final) {
  post(BASE_URI + "/deleteNode",qs.stringify(data),success,error,final,{
    headers: { "content-type": "application/x-www-form-urlencoded" }
  });
}

/**
 * 删除连接
 * @param data
 * @param success
 * @param error
 */
export function deleteLinkAPI(data, success, error, final) {
  post(BASE_URI + "/deleteLink",qs.stringify(data),success,error,final,{
    headers: { "content-type": "application/x-www-form-urlencoded" }
  });
}

/**
 * 导出知识图谱
 * @param data
 * @param success
 * @param error
 */
export function outputFileAPI(data, success, error, final) {
  postForFile(BASE_URI + "/outputFile",qs.stringify(data),success,error,final,{
    headers: { "content-type": "application/x-www-form-urlencoded",responseType: "blob" }
  });
}

/**
 * 存储图谱布局信息
 */
export function saveArrangementAPI(data,success,error,final) {
  let uri = BASE_URI + `/saveLayout?kgID=` + data.kgID;
  post(uri,data.nodesArr,success,error,final);
}
